package com.cxyzy.basic_libs.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import java.util.*

/**
 * 由里及外生成圆环，再逐渐减少，再循环
 */
class CircleViewAnim(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {
    private var mPaint: Paint = Paint()
    private var mTimer: Timer? = null
    private var basePadding = 20
    private var count = 0
    private var maxCount = 20
    private var isIncreasing = true

    init {
        mPaint.color = Color.parseColor("#fab27b")
        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = 10f
        startAnim()
    }

    private fun drawCircle(canvas: Canvas) {
        (0..count).forEach {
            var padding = it * basePadding
            canvas.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), 10f + padding, mPaint)
        }
        if (isIncreasing) {
            count++
        } else {
            count--
        }
        if (count >= maxCount) {
            isIncreasing = false
        } else if (count <= 0) {
            isIncreasing = true
        }
    }

    private fun startAnim() {
        mTimer = Timer()
        mTimer?.schedule(object : TimerTask() {
            override fun run() {
                postInvalidate()
            }
        }, 50, 50)
    }

    override fun onDraw(canvas: Canvas) {
        drawCircle(canvas)
    }
}